<template>
  <el-dialog :title="`${form.id ? '修改' : '添加'}品质得分锁定`" :visible.sync="show" :close-on-click-modal="false" :show-close="!btnLoading" width="500px" append-to-body @closed="$refs.form.resetFields()">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item prop="id" class="hide" />
      <el-form-item prop="countYear" class="hide" />
      <el-form-item prop="cfgStartTime" class="hide" />
      <el-form-item prop="cfgEndTime" class="hide" />
      <el-form-item prop="azStatus" class="hide" />
      <el-form-item label="月份" prop="countMonth">
        <el-date-picker v-model="yearMonth" type="month" placeholder="选择月份" value-format="yyyy-MM" :clearable="false" :disabled="!!form.id" style="width: 100%" />
      </el-form-item>
      <!-- <el-form-item label="起止日期" v-if="form.id">
        <el-date-picker v-model="dateRange" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" style="width: 100%" />
      </el-form-item> -->
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :maxlength="255" style="width: 100%" />
      </el-form-item>
      <el-row>
        <el-col :span="8">
          <el-form-item label="设计状态" prop="designLock">
            <el-switch class="g-switch-in" v-model="form.designLock" :active-value="1" :inactive-value="0" active-text="锁定" inactive-text="未锁" :width="50" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工程状态" prop="engineerLock">
            <el-switch class="g-switch-in" v-model="form.engineerLock" :active-value="1" :inactive-value="0" active-text="锁定" inactive-text="未锁" :width="50" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="品质状态" prop="mergeLock">
            <el-switch class="g-switch-in" v-model="form.mergeLock" :active-value="1" :inactive-value="0" active-text="锁定" inactive-text="未锁" :width="50" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="btnSubmit">确 定</el-button>
      <el-button :disabled="btnLoading" @click="show = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
// 添加或修改品质得分锁定
import { TRD_AZ_ORDER_STATUS as STATUS } from '@/utils/baseData'
import { editFormMixin } from '@/utils/mixin'
import { lockAddUpdate } from '@/api/checkScore/cqEval'
export default {
  mixins: [editFormMixin],
  data() {
    const firstDay = +new Date(new Date().Format('yyyy/MM/01 00:00:00'))
    const lastMonth = new Date(firstDay - 1).Format('yyyy-MM')
    return {
      primaryKey: 'id',
      updateApi: lockAddUpdate,
      form: {
        id: '',
        countMonth: '',
        countYear: '',
        cfgStartTime: '',
        cfgEndTime: '',
        azStatus: STATUS.WAIT,
        remark: '',
        designLock: 0,
        mergeLock: 0,
        engineerLock: 0
      },
      rules: {
        countYear: [{ required: true, message: '请选择年份' }],
        countMonth: [{ required: true, message: '请选择月份' }]
      },
      yearMonth: lastMonth,
      monthPickerOpts: { disabledDate: time => time >= firstDay },
      dateRange: []
    }
  },
  watch: {
    yearMonth: {
      immediate: true,
      handler(val) {
        const yMonth = (val || '').split('-')
        this.form.countYear = yMonth[0] || ''
        this.form.countMonth = yMonth[1] || ''
      }
    },
    dateRange: {
      immediate: true,
      handler(val) {
        const range = val || []
        this.form.cfgStartTime = range[0] || ''
        this.form.cfgEndTime = range[1] || ''
      }
    }
  },
  methods: {
    opened(item = {}) {
      const year = item.countYear
      const month = item.countMonth
      // const sTime = item.cfgStartTime
      // const eTime = item.cfgEndTime
      // this.dateRange = sTime && eTime ? [sTime, eTime] : []

      // 添加时，默认上个月
      if (!item.id) {
        const firstDay = new Date().Format('yyyy/MM/01 00:00:00')
        this.yearMonth = new Date(new Date(firstDay) - 1).Format('yyyy-MM')
        return
      }

      if (year && month) this.yearMonth = `${year}-${('0' + month).substr(-2)}`
    }
  }
}
</script>
